/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package blacksmith.tycoon;

import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author JEWELS
 */
public class QueuedEventHandler {
    String name;
    int[] date_array = new int[2];
    int[] time_array = new int[3];
    short eventID;
    
    protected ConcurrentHashMap hm;
    
    public void init() // runOnce!
    {
        hm = new ConcurrentHashMap(100);
    }
    private String eventFinishTime(int[] dateArray, int[] timeArray, int eventDays, int eventHours)
    {
        timeArray[1] += eventHours;
        dateArray[0] += eventDays;
        if(timeArray[2] >= 24)
        {
            while (timeArray[2] >= 24)
            {
                timeArray[1] -= 24;
                dateArray[0] += 1;
            }
        }
        if(dateArray[0] >= 365){
            while(dateArray[0] >= 365){
                if (dateArray[1] % 4 == 0)
                {
                    if (dateArray[0] == 365)
                        break;
                    if (dateArray[0] > 365)
                    {
                        dateArray[1]++;
                        dateArray[0] -= 366;
                    }
                }
                if (dateArray[1] % 4 != 0)
                {
                    dateArray[1]++;
                    dateArray[0] -= 365;
                }
            }
        }
        String fth = timeHash(dateArray, timeArray);
        return fth;
    }
    private String timeHash(int[] dateArray, int[] timeArray)
    {
        String dth = (dateArray[0] + "-" + dateArray[1] + "-" + timeArray[0] + ":" + timeArray[1]);
        return dth;
    }
    public int checkEvents(int[] dateArray, int[] timeArray) // checks each 
    {
        String dth = (dateArray[0] + "-" + dateArray[1] + "-" + timeArray[0] + ":" + timeArray[1]);
        if(hm.containsKey(dth))
        { // - Start |V| gets eventID
            try {
                String str = hm.get(dth).toString(); // gets eventID from the chashmap
                int eventID = Integer.parseInt(str); // eventID turned from String to int
            } catch (NullPointerException e) {
                System.out.println(" NPE  at   checkEvents() in QueuedEventHandler");
            }
        }
            return eventID; // -gives eventID
    }
    public void checkQueuedEvents(int[] dateArray, int[] timeArray)
    {
        int eventID = checkEvents(dateArray, timeArray);
        if (eventID != 0)
            execute(eventID);
    }
    
    public void addQueuedEvent(int[] dateArray, int[] timeArray, int eventID)
    {
        String datetimehash = (dateArray[0] + "-" + dateArray[1] + "-" + timeArray[0] + ":" + timeArray[1]);
        hm.put(datetimehash, eventID);
    }
    
    private void execute(int eventID)
    {
        switch (eventID) { // switch statement for efficiency when searching for eventID's
            case 1: /* method(); */ break;
        }
    }
    
}
